"""
左侧菜单组件
"""
from PyQt6.QtWidgets import QFrame, QVBoxLayout, QListWidget, QListWidgetItem, QStyle
from PyQt6.QtCore import Qt, pyqtSignal
from .menu_item_widget import MenuItemWidget
from .scan_record_page import ScanRecordPage
from .script_page import ScriptPage
from .auto_select_page import AutoSelectPage


class LeftMenuWidget(QFrame):
    """左侧菜单组件类"""
    
    menu_item_clicked = pyqtSignal(str, object)
    
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setup_ui()
    
    def setup_ui(self):
        """初始化UI"""
        self.setStyleSheet("""
            LeftMenuWidget {
                background-color: #ffffff;
                border-right: 1px solid #e8e8e8;
                min-width: 160px;
                max-width: 160px;
            }
            QListWidget {
                background-color: transparent;
                border: none;
                outline: none;
                padding: 2px 0px;
            }
            QListWidget::item {
                border: none;
                padding: 1px 0px;
                margin: 0px;
                min-height: 24px;
            }
            QListWidget::item:selected {
                background-color: #e6f7ff;
                border-right: 2px solid #1890ff;
            }
            QListWidget::item:hover {
                background-color: #f5f5f5;
            }
        """)
        
        layout = QVBoxLayout(self)
        layout.setContentsMargins(0, 0, 0, 0)
        layout.setSpacing(0)
        
        # 创建菜单列表
        self.menu_list = QListWidget()
        
        # 添加菜单项
        menu_items = [
            ("下单账号管理", QStyle.StandardPixmap.SP_FileDialogListView),
            ("扫货账号管理", QStyle.StandardPixmap.SP_DirIcon),
            ("任务管理", QStyle.StandardPixmap.SP_DirIcon),
            ("自动选品", QStyle.StandardPixmap.SP_DialogApplyButton),
            ("扫货脚本管理", QStyle.StandardPixmap.SP_FileIcon),
            ("扫货记录", QStyle.StandardPixmap.SP_FileDialogBack)
        ]
        
        for text, icon_name in menu_items:
            item = QListWidgetItem()
            item_widget = MenuItemWidget(icon_name, text)
            item.setSizeHint(item_widget.sizeHint())
            self.menu_list.addItem(item)
            self.menu_list.setItemWidget(item, item_widget)
            item.setData(Qt.ItemDataRole.UserRole, text)
        
        layout.addWidget(self.menu_list)
        layout.addStretch()
        
        # 连接信号
        self.menu_list.itemClicked.connect(self._on_item_clicked)
    
    def _on_item_clicked(self, item):
        """处理菜单项点击事件"""
        text = item.data(Qt.ItemDataRole.UserRole)
        if text == "扫货记录":
            # 创建扫货记录页面并发送信号
            scan_record_page = ScanRecordPage()
            self.menu_item_clicked.emit(text, scan_record_page)
        elif text == "扫货脚本管理":
            # 创建扫货脚本管理页面并发送信号
            script_page = ScriptPage()
            self.menu_item_clicked.emit(text, script_page)
        elif text == "自动选品":
            # 创建自动选品页面并发送信号
            auto_select_page = AutoSelectPage()
            self.menu_item_clicked.emit(text, auto_select_page)
        else:
            self.menu_item_clicked.emit(text, None)
